package com.shujia.flink.window

import java.util.Properties

import org.apache.flink.api.common.serialization.SimpleStringSchema
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingEventTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer

object Demo1TImeWindow {

  def main(args: Array[String]): Unit = {


    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "master:9092,node1:9092,node2:9092")


    val consumer = new FlinkKafkaConsumer[String]("test_topic1", new SimpleStringSchema(), properties)

    consumer.setStartFromLatest()

    val kafkaDS: DataStream[String] = env.addSource(consumer)


    val kvDS: KeyedStream[(String, Int), String] = kafkaDS.map((_, 1)).keyBy(_._1)


    /**
      * 时间窗口
      * TumblingProcessingTimeWindows  滚动处理时间窗口
      * TumblingEventTimeWindows   滚动事件时间窗口
      * SlidingProcessingTimeWindows 滑动处理时间窗口
      * SlidingEventTimeWindows  滑动 事件事件窗口
      *
      */

    kvDS
      .window(SlidingProcessingTimeWindows.of(Time.seconds(5), Time.seconds(2)))
      .sum(1)
      .print()


    env.execute()


  }

}
